package com.verisette.ncos.service.card.bizunit;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;

public class CreatePurchaseOrderBU  extends CommonBusinessUnit {
	Logger logger = Logger.getLogger(CreatePurchaseOrderBU.class);
	
	public void execute(ProcessContext processContext) throws ProcessException {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );

		TransCustomerOrder order = (TransCustomerOrder)processContext.getValueObject();
		
		order = (TransCustomerOrder) commonDAO.findById(
						TransCustomerOrder.class, order.getOrderId());

		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute next" );
		
		MasterUserProfile approver2 = (MasterUserProfile)processContext.getUserBean();
		String userPDFPathDir = ProjectConstant.APP_ORDER_PO_FOLDER;
		String fullPDFPathFile = userPDFPathDir+"\\"+(new SimpleDateFormat("'PO_'ddMMyyyy'_'HHmmss'_'SSS", ProjectConstant.defaultLocale)).format(new Date())+".pdf";
		Document document = new Document(PageSize.A4, 20f, 20f, 20f, 20f);
		PdfWriter writer = null;
		
		File output=null;
		
		try{
			final String companyName = "บริษัท ไปรษณีย์ไทย จำกัด";
			MasterUserProfile creator = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, order.getCreatedUspId());
			MasterUserProfile printer = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, ProjectConstant.DEFAULT_PRINTER_USP_ID);
//			MasterUserProfile approver1 = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, order.getApprovedUspId());
			
			File dir = new File(userPDFPathDir);
			if(!dir.exists()) dir.mkdirs();
			
			output = new File(fullPDFPathFile);
			writer = PdfWriter.getInstance(document, new FileOutputStream(output));
			
			FontFactory.register(ProjectConstant.FONT_PSLPRO_NORMAL, ProjectConstant.FONT_NAME_PSLPRO_NORMAL);
			FontFactory.register(ProjectConstant.FONT_PSLPRO_BOLD, ProjectConstant.FONT_NAME_PSLPRO_BOLD);
			FontFactory.register(ProjectConstant.FONT_PSLPRO_ITALIC, ProjectConstant.FONT_NAME_PSLPRO_ITALIC);
			
			System.out.println("PageSize {" + PageSize.A4.getWidth() + "," + PageSize.A4.getHeight() + "}");
			
	        document.open();
	        
	        Image title = Image.getInstance(String.format(ProjectConstant.APP_IMAGES_FOLDER + "\\BG\\OrderTitle.jpg"));
	        
	        title.scalePercent(65);
	        document.add(title);
	        
    		BaseFont font = BaseFont.createFont(ProjectConstant.FONT_PSLPRO_NORMAL, ProjectConstant.EXPORT_ENCODE_TIS620, BaseFont.EMBEDDED);
    		BaseFont fontBold = BaseFont.createFont(ProjectConstant.FONT_PSLPRO_BOLD, ProjectConstant.EXPORT_ENCODE_TIS620, BaseFont.EMBEDDED);
    		
    		PdfContentByte over = writer.getDirectContent();
    		over.saveState();                         
	        over.beginText();
    		
	        //OrderTitle #################################################
    		Rectangle recOrderTitle = new Rectangle(30, 740, 292.5f, 690);
    		recOrderTitle.setBorder(Image.BOX);
    		recOrderTitle.setBorderWidth(0.5f);
    		recOrderTitle.setBorderColor(BaseColor.BLACK);
	        document.add(recOrderTitle);
	        
	        over.setFontAndSize(fontBold,20);    		
    		over.showTextAligned(Element.ALIGN_CENTER, "ใบสั่งซื้อ", 166.25f, 720, 0);
    		over.showTextAligned(Element.ALIGN_CENTER, "Purchase Order", 166.25f, 700, 0);
    		
    		//OrderNo #################################################
    		Rectangle recOrderNo = new Rectangle(297.5f, 740, 428.75f, 690);
    		recOrderNo.setBorder(Image.BOX);
    		recOrderNo.setBorderWidth(0.5f);
    		recOrderNo.setBorderColor(BaseColor.BLACK);
	        document.add(recOrderNo);
    		over.setFontAndSize(fontBold,16);    		
    		over.showTextAligned(Element.ALIGN_LEFT, "เลขที่ใบสั่งซื้อ:", 300.5f, 695, 0);
    		over.setFontAndSize(font,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, order.getOrderNoDisp(), 360.5f, 695, 0);

    		//OrderDate #################################################
    		Rectangle recOrderDate = new Rectangle(433.75f, 740, 565, 690);
    		recOrderDate.setBorder(Image.BOX);
    		recOrderDate.setBorderWidth(0.5f);
    		recOrderDate.setBorderColor(BaseColor.BLACK);
	        document.add(recOrderDate);
    		over.setFontAndSize(fontBold,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 436.75f, 695, 0);
    		over.setFontAndSize(font,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, order.getCreatedDateDisp(), 456.75f, 695, 0);

    		Rectangle rec = null;
    		ColumnText ct = null;
    		
    		//Customer profile #################################################
    		rec = new Rectangle(30, 680, 292.5f, 660);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
	        
    		rec = new Rectangle(30, 680, 292.5f, 570);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);

    		over.setFontAndSize(fontBold,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, "รายละเอียดลูกค้า", 32, 665, 0);
    		
    		over.showTextAligned(Element.ALIGN_LEFT, "ลูกค้า", 32, 645, 0);
    		over.setFontAndSize(font,16);    	
    		over.showTextAligned(Element.ALIGN_LEFT, creator.getFullNameWithTitleTh(), 62, 645, 0);

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "บริษัท", 32, 625, 0);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_LEFT, companyName, 62, 625, 0);

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "ที่อยู่", 32, 605, 0);
    		
    		over.saveState();
    		ct = new ColumnText(over);
    		ct.setSimpleColumn(new Phrase(new Chunk(
//    				"111 หมู่ 3 ถนนแจ้งวัฒนะ แขวงทุ่งสองห้อง เขตหลักสี่ กรุงเทพฯ 10210-0299",
    				creator.getMasterBranch().getBranchAddrThDisp(), 
    				new Font(font, 16))), 
    				62, 625, 292.5f, 570, 
    				20, Element.ALIGN_LEFT);
    		ct.go();
    		over.restoreState();
    		
    		//Delivery profile #################################################
    		rec = new Rectangle(297.5f, 680, 565, 660);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
	        
    		rec = new Rectangle(297.5f, 680, 565, 570);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "สถานที่ส่ง", 299.5f, 665, 0);
    		
    		over.showTextAligned(Element.ALIGN_LEFT, "ลูกค้า", 299.5f, 645, 0);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_LEFT, creator.getFullNameWithTitleTh(), 329.5f, 645, 0);

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "บริษัท", 299.5f, 625, 0);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_LEFT, companyName, 329.5f, 625, 0);

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "ที่อยู่", 299.5f, 605, 0);
    		
    		over.saveState();
    		ct = new ColumnText(over);
    		ct.setSimpleColumn(new Phrase(new Chunk(
//    				"111 หมู่ 3 ถนนแจ้งวัฒนะ แขวงทุ่งสองห้อง เขตหลักสี่ กรุงเทพฯ 10210-0299", 
    				creator.getMasterBranch().getBranchAddrThDisp(),
    				new Font(font, 16))), 
    				329.5f, 625, 565, 570, 
    				20, Element.ALIGN_LEFT);
    		ct.go();
    		over.restoreState();
    		
    		//condition #################################################

    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "เงื่อนไข", 32, 545, 0);
    		
    		over.showTextAligned(Element.ALIGN_LEFT, "วันกำหนดส่ง", 32, 505, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 167, 505, 0);
//    		over.showTextAligned(Element.ALIGN_CENTER, order.getOrderDateDisp(), 232, 505, 0);
    		
    		over.showTextAligned(Element.ALIGN_LEFT, "อ้างอิงใบเสนอราคาเลขที่", 32, 485, 0);
    		
	        
	        //Product profile list header #################################################
	        //Product Code header
    		rec = new Rectangle(30, 470, 80, 450);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "รหัสสินค้า", 55, 455, 0);

	        //Order detail header
    		rec = new Rectangle(80, 470, 365, 450);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "รายการ", 222.5f, 455, 0);

	        //Unit header
    		rec = new Rectangle(365, 470, 415, 450);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "หน่วย", 390, 455, 0);

	        //Amount header
    		rec = new Rectangle(415, 470, 465, 450);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "จำนวน", 440, 455, 0);

	        //Price per unit header
    		rec = new Rectangle(465, 470, 515, 450);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "หน่วยละ", 490, 455, 0);

	        //price header
    		rec = new Rectangle(515, 470, 565, 450);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
    		rec.setBackgroundColor(BaseColor.LIGHT_GRAY);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "จำนวนเงิน", 540, 455, 0);

	        //Product profile list detail #################################################
    		over.setFontAndSize(font,16);
    		
	        //Product Code #2
    		rec = new Rectangle(30, 450, 80, 390);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, order.getProductCodeDisp(), 55, 435, 0);

	        //Order detail #2
    		rec = new Rectangle(80, 450, 365, 390);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_LEFT, order.getOrderOwnerName(), 82, 435, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, order.getPdfPath(), 82, 400, 0);

	        //Unit #2
    		rec = new Rectangle(365, 450, 415, 390);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, "ใบ", 390, 435, 0);

	        //Amount #2
    		rec = new Rectangle(415, 450, 465, 390);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getQtyDisp(), 463, 435, 0);

	        //Price per unit #2
    		rec = new Rectangle(465, 450, 515, 390);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getPricePerUnitDisp(), 513, 435, 0);

	        //price #2
    		rec = new Rectangle(515, 450, 565, 390);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getPriceDisp(), 563, 435, 0);
    		
	        //Product profile list notice #################################################
    		rec = new Rectangle(30, 390, 80, 370);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "*หมายเหตุ", 55, 375, 0);

    		rec = new Rectangle(80, 390, 415, 370);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);

    		rec = new Rectangle(415, 390, 515, 370);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_RIGHT, "รวมเงิน", 513, 375, 0);

    		rec = new Rectangle(515, 390, 565, 370);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getPriceDisp(), 563, 375, 0);

	        //Product profile list vat #################################################
    		rec = new Rectangle(30, 370, 415, 350);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);

    		rec = new Rectangle(415, 370, 515, 350);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_RIGHT, "ภาษีมูลค่าเพิ่ม 7%", 513, 355, 0);

    		rec = new Rectangle(515, 370, 565, 350);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_RIGHT, "7.00", 563, 355, 0);

	        //Product profile list total #################################################
    		rec = new Rectangle(30, 350, 565, 330);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "ตัวอักษร", 55, 335, 0);
	        
    		rec = new Rectangle(80, 350, 415, 330);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_CENTER, order.getPriceIncVatSpellDisp(), 222.5f, 335, 0);

    		rec = new Rectangle(415, 350, 515, 330);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.showTextAligned(Element.ALIGN_RIGHT, "จำนวนเงิน", 513, 335, 0);

    		rec = new Rectangle(515, 350, 565, 330);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_RIGHT, order.getPriceIncVatDisp(), 563, 335, 0);
	        
	        //Notice the condition #################################################
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "เงื่อนไขอื่นๆ", 32, 305, 0);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_LEFT, "1. โปรดระบุเลขที่ใบสั่งซื้อข้างต้น ในใบส่งของทุกฉบับ", 33, 285, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "2. การวางบิลและการรับเช็ค เป็นไปตามกำหนดเวลาที่บริษัทกำหนดไว้", 33, 265, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "3. ในการวางบิลเพื่อเรียกเก็บ ให้แนบสำเนาใบสั่งซื้อกำกับมาด้วย", 33, 245, 0);
	        
	        //Attendee signature #################################################
	        //User
    		rec = new Rectangle(30, 230, 160, 150);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
//    		over.showTextAligned(Element.ALIGN_CENTER, "User", 97, 205, 0);
	        over.showTextAligned(Element.ALIGN_CENTER, creator.getFullNameWithTitleTh(), 97, 205, 0);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "ผู้สั่งซื้อ", 97, 175, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 32, 155, 0);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_CENTER, order.getCreatedDateDisp(), 97, 155, 0);

	        //Printer
    		rec = new Rectangle(165, 230, 295, 150);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
//    		over.showTextAligned(Element.ALIGN_CENTER, "Admin", 232, 205, 0);
	        over.showTextAligned(Element.ALIGN_CENTER, printer.getFullNameWithTitleTh(), 232, 205, 0);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "ผู้รับใบสั่งซื้อ", 232, 175, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 167, 155, 0);
    		over.setFontAndSize(font,16);
    		over.showTextAligned(Element.ALIGN_CENTER, order.getCreatedDateDisp(), 232, 155, 0);

	        //Approver
    		rec = new Rectangle(300, 230, 430, 150);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
//    		over.showTextAligned(Element.ALIGN_CENTER, "Approver #1", 367, 212.5f, 0);
//    		over.showTextAligned(Element.ALIGN_CENTER, "Approver #2", 367, 197.5f, 0);
//    		over.showTextAligned(Element.ALIGN_CENTER, approver1.getFullNameWithTitleTh(), 367, 212.5f, 0);
//    		over.showTextAligned(Element.ALIGN_CENTER, approver2.getFullNameWithTitleTh(), 367, 197.5f, 0);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "ผู้อนุมัติ", 367, 175, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 302, 155, 0);
    		over.setFontAndSize(font,16);
//    		over.showTextAligned(Element.ALIGN_CENTER, order.getApproved02DateDisp(), 367, 155, 0);

	        //Receiver
    		rec = new Rectangle(435, 230, 565, 150);
    		rec.setBorder(Image.BOX);
    		rec.setBorderWidth(0.5f);
    		rec.setBorderColor(BaseColor.BLACK);
	        document.add(rec);
    		over.setFontAndSize(fontBold,16);
    		over.showTextAligned(Element.ALIGN_CENTER, "ผู้รับสินค้า", 502, 175, 0);
    		over.showTextAligned(Element.ALIGN_LEFT, "วันที่", 437, 155, 0);
//    		over.showTextAligned(Element.ALIGN_CENTER, order.getOrderDateDisp(), 502, 155, 0);
    		
	        over.endText();
	        over.restoreState();        
	        
	        document.close();
		}catch(Exception ex){
			logger.error(ex.getStackTrace());
			ex.printStackTrace();
		}finally{
			if(writer!=null) try{ writer.close(); }catch(Exception ex){ logger.error("writer.close() fail."); }
		}
		
		if(output!=null){
			order = (TransCustomerOrder)commonDAO.findById(TransCustomerOrder.class, order.getOrderId());
			order.setPoPath(output.getName());
			commonDAO.save(order);
		}
		
		processContext.setValueObject(order);
		
		if(logger.isDebugEnabled()){
			logger.debug("fullPDFPathFile::" + fullPDFPathFile);
			logger.debug("PDF (java.io.File)output::" + output);
		}
	}
}
